#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=500005;

struct eg{
    int t;
    LL w;
    eg(){}
    eg(int t,LL w){this->t=t;this->w=w;}
};
int n;
int root;
vector<eg> edges[N];
int fath[N];
int sum[N];


LL res=0;

void dfs0(int r){
    if(edges[r].size()==1){
        return;
    }
    for(auto e:edges[r]){
        if(e.t==fath[r]) continue;
        int son=e.t;
        fath[son]=r;
        dfs0(son);
    }
}
void dfs1(int r){
    if(edges[r].size()==1) return;
    LL maxw=0;
    for(auto e:edges[r]){
        if(e.t==fath[r]) continue;
        dfs1(e.t);
        maxw = max(maxw,sum[e.t]+e.w);
    }
    for(auto e:edges[r]){
        res+=maxw-(sum[e.t]+e.w);
    }
    sum[r]=maxw;
}

int main()
{
    scanf("%d%d",&n,&root);
    int n1,n2;
    LL w;
    for(int i=1;i<n;i++)
    {
        scanf("%d%d%lld",&n1,&n2,&w);
        edges[n1].emplace_back(eg(n2,w));
        edges[n2].emplace_back(eg(n1,w));
    }
    memset(fath,0,sizeof(fath));
    memset(sum,0,sizeof(sum));
    dfs0(root);
    dfs1(root);
    printf("%lld",res);
    return 0;
}